<?php

namespace app\index\controller;
use model\PolicyModel;
use think\Db;
use think\Exception;

/**
 * Desc: 业务员
 * Class SaleMan
 * Created By Doyi
 * Date: 2018/10/30
 */
class SaleMan extends CommonController
{
    public $request;

    public function __construct(\think\Request $request)
    {
        parent::__construct($request);
        $this->request = $request->param();
    }

    /**
     * 业务员列表接口
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saleManIndex()
    {
        if($this->request['type'] == 2){
            $where['insurant.delete_ts'] = 0;
            $where['insurant.status'] = ['not in',0];
            $info = Db::table('insurant')->where($where)->join('policy','policy.p_id=insurant.p_id','left')
                ->field('insurant.status,policy.created_at,policy.policy_num,insurant.p_id')
                ->order('insurant.status')
                ->select();
        }else{
            $where['delete_ts'] = 0;
            $info = Db::table('policy')->where($where)->field('policy_status,created_at,policy_num,policy.p_id')->order('policy_status')->select();
        }
        return $this->response->json(['data'=>$info,'code'=>self::SUCCESS_CODE,'msg'=>'']);
    }


   /**
    * 业务员点击查看保单详情 （物权人通用）
    * @return \think\Response
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\ModelNotFoundException
    * @throws \think\exception\DbException
    */
   public function getPolicyOrInsurant()
   {
       $id = isset($this->request['p_id']) ? $this->request['p_id'] : '';
       $type = isset($this->request['type']) ? $this->request['type'] : 1;
       $PolicyModel = new PolicyModel();
       $res = $PolicyModel->where('p_id',$id)->find();
       if($type == 1){
           $info = Db::table('policy_item')->where('policy_num',$res['policy_num'])->find();
           if($res['policy_status'] == 0){
                $where = [
                    'status'=>1,
                    'p_id'=>$res['p_id']
                ];
                $msg = Db::table('return_policy')->where($where)->select();
                $policy_return = [];
                $goods_return = [];
                $under_return = [];
                $situation_return = [];
                $info_return = [];
                $certificate_return = [];
                foreach($msg as $k=>$v){
                    if($v['policy_return']){
                        $policy_return[] = $v['policy_return'];
                    }
                    if($v['goods_return']){
                        $goods_return[] = $v['goods_return'];
                    }
                    if($v['under_return']){
                        $under_return[] = $v['under_return'];
                    }
                    if($v['situation_return']){
                        $situation_return[] = $v['situation_return'];
                    }
                    if($v['info_return']){
                        $info_return[] = $v['info_return'];
                    }
                    if($v['certificate_return']){
                        $certificate_return[] = $v['certificate_return'];
                    }
                }
                $return_msg['policy_return'] =  $goods_return;
                $return_msg['goods_return'] =  $goods_return;
                $return_msg['under_return'] =  $under_return;
                $return_msg['situation_return'] =  $situation_return;
                $return_msg['info_return'] =  $info_return;
                $return_msg['certificate_return'] =  $certificate_return;
               $data['return_msg'] = $return_msg;
           }
           $info['policy_num'] = $res['policy_num'];
           $info['report_num'] = $res['report_num'];
           $info['insurance_type'] = $res['insurance_type'];
           $info['policy_status'] = $res['policy_status'];
           if($res['recordor_id'] == 0){
               $info['company_end_at'] = $res['company_end_at'];
           }else{
               $userInfo = Db::table('user_info')->where('ui_id',$res['recordor_id'])->find();
               $recorder['username'] = $userInfo['username'];
               $recorder['mobile'] = $userInfo['mobile'];
               $recorder['recoder_type'] = $userInfo['recoder_type'];
               $name = Db::table('recorder_type')->where('id',$recorder['recoder_type'])->find();
               $recorder['recoder_type_name'] = $name['name'];
               $recorder['end_at'] = $res['end_at'];
               $data['recordor'] = $recorder;
           }
           $data['goods_info'] = $info;
       }else{
           $info = Db::table('insurant')->where('p_id',$id)->find();
           if($info['status'] == 3){
               $where = [
                   'status'=>1,
                   'i_id'=>$id,
               ];
               $msg = Db::table('return_insurant')->where($where)->select();
               $policy_return = [];
               $goods_return = [];
               $under_return = [];
               $situation_return = [];
               $info_return = [];
               foreach($msg as $k=>$v){
                   if($v['join_price']){
                       $policy_return[] = $v['join_price'];
                   }
                   if($v['look_goods_info']){
                       $goods_return[] = $v['look_goods_info'];
                   }
                   if($v['bol_info']){
                       $under_return[] = $v['bol_info'];
                   }
                   if($v['receive_money']){
                       $situation_return[] = $v['receive_money'];
                   }
                   if($v['auth_report']){
                       $info_return[] = $v['auth_report'];
                   }
               }
               $return_msg['join_price'] =  $goods_return;
               $return_msg['look_goods_info'] =  $goods_return;
               $return_msg['bol_info'] =  $under_return;
               $return_msg['receive_money'] =  $situation_return;
               $return_msg['auth_report'] =  $info_return;
               $data['return_msg'] = $return_msg;
           }
           $info['policy_num'] = $res['policy_num'];
           $info['report_num'] = $res['report_num'];
           $info['insurance_type'] = $res['insurance_type'];
           $info['policy_status'] = $res['policy_status'];
           $data['goods_info'] = $info;
       }
       return $this->response->json(['data'=>$data,'code'=>self::SUCCESS_CODE,'msg'=>'']);

   }

    /**
     * 物权人通过
     * @return \think\Response
     */
    public function pass_insurant()
    {
        $i_id = $this->request['i_id'];
        try{
            Db::startTrans();
            Db::table('insurant')->where('i_id',$i_id)->update(['status'=>4]);
            $where = [
                'i_id'=>$i_id,
                'auth_id' => $this->uid,
                'form_table'=>'user_info',
                'status'=>1
            ];
            Db::table('return_insurant')->where($where)->update(['status' => 2]);
            Db::commit();
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }

    }


    /**
     * 业务员驳回物权信息
     * @return \think\Response
     */
    public function returnInurant()
    {
        $data = [
            'join_price'=>isset($this->request['join_price']) ? $this->request['join_price'] : '',
            'look_goods_info'=>isset($this->request['look_goods_info']) ? $this->request['look_goods_info'] : '',
            'bol_info'=>isset($this->request['bol_info']) ? $this->request['bol_info'] : '',
            'receive_money'=>isset($this->request['receive_money']) ? $this->request['receive_money'] : '',
            'auth_report'=>isset($this->request['auth_report']) ? $this->request['auth_report'] : '',
            'auth_id'=>$this->uid,
            'form_table'=>'user_info',
            'i_id'=>isset($this->request['i_id']) ? $this->request['i_id'] : '',
            'created_at'=>date('Y-m-d H:i:s',time()),
        ];
        try{
            Db::startTrans();
            Db::table('return_insurant')->insert($data);
            Db::table('insurant')->where('i_id',$this->request['i_id'])->update(['status' => 3]);
            Db::commit();
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }
    }

    /**
     * 业务员驳回保险人员
     * @return \think\Response
     */
    public function returnPolicy()
    {
        $data = [
            'insurance_type'=>isset($this->request['insurance_type']) ? $this->request['insurance_type'] : '',
            'good_type'=>isset($this->request['good_type']) ? $this->request['good_type'] : '',
            'damaged_goods'=>isset($this->request['damaged_goods']) ? $this->request['damaged_goods'] : '',
            'now_goods'=>isset($this->request['now_goods']) ? $this->request['now_goods'] : '',
            'rem_info'=>isset($this->request['rem_info']) ? $this->request['rem_info'] : '',
            'author_report'=>isset($this->request['author_report']) ? $this->request['author_report'] : '',
            'extend_table'=>'user_info',
            'extend_id'=>$this->uid,
            'p_id'=>isset($this->request['p_id']) ? $this->request['p_id'] : '',
            'created_at'=>date('Y-m-d H:i:s',time()),
        ];
        try{
            Db::startTrans();
            $res = Db::table('return_policy')->insert($data);
            Db::table('policy')->where('p_id',$this->request['p_id'])->update(['policy_status' => 0]);
            Db::commit();
            return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }
    }


    /**
     * 保险通过
     * @return \think\Response
     */
    public function pass_policy()
    {
        $p_id = $this->request['p_id'];
        try{
            Db::startTrans();
            Db::table('policy')->where('p_id',$p_id)->update(['status'=>5]);
            $info = Db::table('policy')->where('p_id',$p_id)->find();
            $data = [
                'p_id'=>$p_id,
                'ui_id'=>$info['remer_id'],
                'status'=>0,
                'created_at'=>date('Y-m-d H:i:s',time())
            ];
            Db::table('insurant')->insert($data);
            $where = [
                'p_id'=>$p_id,
                'extend_id' => $this->uid,
                'extend_table'=>'user_info',
                'status'=>1
            ];
            Db::table('return_policy')->where($where)->update(['status' => 2]);
            Db::commit();
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }
    }

}